#!/bin/bash

source env.sh
source parameters.sh
source functions.sh

mylog=gen.init.log
rm -f $mylog
cwd=`pwd`
cp -a $init_tool_dir/dihedral.table .
rm -fr tools
mkdir -p tools
mv dihedral.table ./tools
init_tool_dir=$cwd/tools
make -C $init_tool_dir/dihedral.table clean &> /dev/null
make -j -C $init_tool_dir/dihedral.table &> /dev/null

for angle in `seq $init_interval_start $init_center_angle_interval $init_interval_end`
do
    pa=`printf %+04d $angle`
    echo "# doing angle $pa" 
    workdir=./init.conf.$pa
    if test -d $workdir; then
	mv $workdir $workdir.`date +%s`
    fi
    mkdir $workdir
    cd $workdir    
    
    cp $init_gmx_template/conf.gro .
    cp $init_gmx_template/grompp.mdp .
    cp $init_gmx_template/topol.top .
    cp $init_gmx_template/angle.ndx .
    test -f $init_gmx_template/index.ndx && cp $init_gmx_template/index.ndx .

    $init_tool_dir/dihedral.table/constraint -k $init_constraint_k -p $angle &>> $mylog
    
    set_parameters_init_warm grompp.mdp
    if test -f $init_gmx_template/index.ndx; then
	grompp -n index.ndx &>> $mylog
    else
	grompp &>> $mylog
    fi
    mdrun -v &>> $mylog

    mv -f confout.gro conf.gro
    grocleanit
    
    set_parameters_init grompp.mdp
    if test -f $init_gmx_template/index.ndx; then
	grompp -n index.ndx &>> $mylog
    else
	grompp &>> $mylog
    fi
    mdrun -v &>> $mylog
    
    cd $cwd
done